package com.wangcang.widgetlibrary.dialog;


import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;


/**
 * dialog工具类封装
 * Created by ShineF on 2017/6/19 0019.
 */

public class DialogTools {


    private static DialogTools mDialogTools;
    private Dialog mDialog;
    private ConfirmListener mListener;


    public static DialogTools getInstance() {
        if (null == mDialogTools) {
            synchronized (DialogTools.class) {
                if (null == mDialogTools) {
                    mDialogTools = new DialogTools();
                }
            }
        }
        return mDialogTools;
    }


    public interface ConfirmListener {
        void setConfirmListener(Dialog dialog, View view, String input);
    }

    public void setConfirmListener(ConfirmListener listener) {
        this.mListener = listener;
    }


    /**
     * 带取消确定的dialog
     *
     * @param title         标题
     * @param content       提示内容
     * @param cancelOutSide 点击窗口外面是否消失
     * @param listener      点击确定的回调, 如果是null则dismiss dialog
     */
    public void showCustomDialogPrompt(Context context, String title, String content, boolean cancelOutSide, final ConfirmListener listener) {
        View view = View.inflate(context, R.layout.view_custom_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        contentText.setText(content);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }


    /**
     * 只有一个按钮的提示框
     *
     * @param title         标题
     * @param content       提示内容
     * @param cancelOutSide 点击外部是否消失
     * @param listener      按钮点击的回调事件, 传null默认dismiss dialog
     */
    public void showCustomWarning(Context context, String title, String content, boolean cancelOutSide, final ConfirmListener listener) {
        View view = View.inflate(context, R.layout.view_custom_2_dialog, null);
        TextView contentText = (TextView) view.findViewById(R.id.tv_show_content);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        contentText.setText(content);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.setConfirmListener(mDialog, v, "");
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }


    /**
     * 带有输入框的Dialog
     * @param context 上下文
     * @param title 标题
     * @param cancelOutSide 点击外面是否消失
     * @param listener 按钮监听
     */
    public void showCustomInput(Context context, String title, boolean cancelOutSide, final ConfirmListener listener) {
        View view = View.inflate(context, R.layout.view_custom_3_dialog, null);
        Button cancelBtn = (Button) view.findViewById(R.id.btn_cancel);
        Button confirmButton = (Button) view.findViewById(R.id.btn_confirm);
        TextView titleText = (TextView) view.findViewById(R.id.tv_title);
        final EditText etInput = (EditText) view.findViewById(R.id.et_input);
        mDialog = new AlertDialog.Builder(context)
                .setView(view)
                .create();
        mDialog.setCanceledOnTouchOutside(cancelOutSide);
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        WindowManager.LayoutParams params = mDialog.getWindow().getAttributes();
        params.width = context.getResources().getDisplayMetrics().widthPixels * 5 / 6;
        mDialog.getWindow().setAttributes(params);
        if (null != title) {
            titleText.setText(title);
        }
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    String inputContent = etInput.getText().toString().trim();
                    listener.setConfirmListener(mDialog, v, inputContent);
                } else {
                    mDialog.dismiss();
                }
            }
        });
    }

}
